package org.manager.properties;

import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.math.NumberUtils;
import org.manager.generic.web.JsonViewUtil;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

@Controller
@RequestMapping("/property")
public class PropertyController {
	
	@Resource(name="propertyRepository")
	private PropertyRepository pr;

	private static final String propertyAllField[] = new String[]{"id","englishName","chineseName","level",
			"code","type","kind","propertiesCount"};
	
	/**
	 * 分页取出参数数据.
	 * @param property 用来查询的参数示例.
	 * @param start	当前要显示的页码.
	 * @param limit 每页显示的信息数量.
	 * @return 视图和模型.
	 */
	@RequestMapping(params="method=page")
	public ModelAndView page(Property property, String start, String limit){
		
		int istart = 0;
		int ilimit = 10;
		if(NumberUtils.isNumber(start)){
			istart = NumberUtils.toInt(start);
			if(istart > 1){
				istart = istart-1;
			}
		}
		
		if(NumberUtils.isNumber(limit) && NumberUtils.toInt(limit)>0){
			ilimit = NumberUtils.toInt(limit);
		}
		
		Map<String,Object> map = new HashMap<String,Object>();
		
		//查询满足条件的总行数.
		map.put("totalProperty", pr.totalRecord(property));
		//查询当页数据.
		map.put( "data", pr.findPageProperty(property, istart/1, ilimit/1));
		//设置当前页.
		map.put("start", istart);
		//设置步长.
		map.put("limit", ilimit);
		
		return JsonViewUtil.getSuccessViewWithIncludProperty(map, propertyAllField);
	}
	
	@RequestMapping(params="method=all")
	public ModelAndView all(){
		return JsonViewUtil.getSuccessViewWithIncludProperty(pr.loadAll(), propertyAllField);
	}
	
	@RequestMapping(params="method=add", method=RequestMethod.POST)
	public ModelAndView add(Property property){
		String id = (String)pr.save(property);
		if(StringUtils.isNotEmpty(id)){
			return JsonViewUtil.getSuccessViewWithIncludProperty(JsonViewUtil.getSuccessMap().put("propertyId", id), new String[0]);
		}else{
			return JsonViewUtil.getFailureView();
		}
	}
	
	@RequestMapping(params="method=loadById")
	public ModelAndView loadById(String id){
		Property property = pr.get(id);
		return JsonViewUtil.getSuccessViewWithIncludProperty(property, propertyAllField);
	}
	
	@RequestMapping(params="method=modify", method=RequestMethod.POST)
	public ModelAndView modify(Property property){
		if(property != null && StringUtils.isNotEmpty(property.getId())){
			pr.update(property);
			return JsonViewUtil.getSuccessView();
		}else{
			return JsonViewUtil.getFailureView();
		}
	}
	
	@RequestMapping(params="method=delete")
	public ModelAndView del(String ids){
		String id[] = ids.split(",");
		if(StringUtils.isNotBlank(ids)){
			for(String s : id){
				pr.deleteByKey(s);
			}
			return JsonViewUtil.getSuccessView();
		}
		return JsonViewUtil.getFailureView();
	}
	
	
}
